/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import models.Course;
import models.User;
import org.hibernate.Session;
import service.CourseDao;
import service.HibernateUtil;
import service.UserDao;

/**
 *
 * @author Aster
 */
public class TutorController extends HttpServlet {

    HibernateUtil HU = new HibernateUtil();
    Session sessie = HU.openSession();
    UserDao ud = new UserDao(sessie);

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {

        boolean tutor = false;
        boolean admin = false;
        long userID = 0;
        String adminstring = "hallo";
        for (Cookie cookie : request.getCookies()) {
            if (cookie.getName().equals("status")) {
                if (cookie.getValue().equals("Tutor")) {
                    tutor = true;
                } else if (cookie.getValue().equals("Admin")) {
                    tutor = true;
                    admin = true;
                    adminstring = "true";
                }
            } else if (cookie.getName().equals("userID")) {
                userID = Long.parseLong(cookie.getValue());
            }
        }
        if (tutor == true) {
            HttpSession session = request.getSession(true);

            //stuk om de aangewezen cursussen van docent op te halen
            try {
                User user = ud.get(userID);//dit moet dynamisch worden
                
                if (user.getCourses() != null) {
                    session.setAttribute("courses", user.getCourses());
                    session.setAttribute("name", user.getFirstName() + " " + user.getLastName());
                    session.setAttribute("theUser", user);
                    DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                    String birthDateString = formatter.format(user.getBirthDate());
                    System.out.println("birthdate: " + birthDateString);
                    request.setAttribute("birthDate", birthDateString);
                }
            } catch (Exception ex) {
                System.out.println(ex);
            }
            Locale list[] = SimpleDateFormat.getAvailableLocales();
            //System.out.println("country: " +list[0].getDisplayLanguage());
            session.setAttribute("countries", list);
            session.setAttribute("admin", adminstring);
            forward(request, response);
        } else {
            response.sendRedirect("/");
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        HttpSession sessie = request.getSession(true);
        User user = (User) sessie.getAttribute("theUser");
        user.setAddress(request.getParameter("address"));
        user.setCountry(request.getParameter("country"));
        if (request.getParameter("birthdate") != null) {
            try {

                String str_date = request.getParameter("birthdate");
                java.util.Date date = new SimpleDateFormat("yyyy-MM-dd").parse(str_date);
                System.out.println(date + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
                user.setBirthDate(date);

            } catch (ParseException ex) {
                Logger.getLogger(UserController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        user.setCity(request.getParameter("city"));
        user.setEmail(request.getParameter("email"));
        user.setFirstName(request.getParameter("firstname"));
        user.setGender(request.getParameter("gender"));
        user.setPhoneNumber(request.getParameter("phonenumber"));
        user.setLastName(request.getParameter("lastname"));
        user.setUserName(request.getParameter("username"));
        user.setZipCode(request.getParameter("zipcode"));
        user.setPassWord(request.getParameter("password"));
        ud.update(user);
        forward(request,response);
    }

    private void forward(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String address = "/tutor.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }
}
